<template>
  <div class="box" style="height: 500px">
    <div class="xtx-slider" @mouseover="clearTimer" @mouseleave="startTimer">
      <!-- 图片列表 -->
      <ul class="slider-body">
        <!--
          fade: 当fade类名存在 当前图片就显示 不存在就不显示
         -->
        <li
          class="slider-item"
          v-for="(item, i) in sliders"
          :key="i"
          :class="{ fade: curIndex === i }"
        >
          <img v-ImgLazy="item.imgUrl" alt="" />
        </li>
      </ul>
      <div class="slider-left" :class="{'slider-left1':leftShow}" @click="onLeftFn"></div>
      <div class="slider-reght" @click="onRightFn"></div>
      <!-- 圆圈切换按钮 -->
      <div class="slider-indicator">
        <span
          @click="curIndex = i"
          v-for="(item, i) in sliders"
          :key="i"
          :class="{ active: curIndex === i }"
        ></span>
      </div>
    </div>
  </div>
</template>

<script>
/**
 * 目标：点击圆圈按钮 实现对应图片的切换
 * 思路：
 *   1. 图片和圆圈按钮数量是一样的 下标值是对应的
 *   2. 记录一下当前点击的是哪一项
 *   3. 需要根据记录下来的下标值 去配合:class 控制fade这个类名是否应该显示
 */
import { ref, onMounted, onUnmounted } from 'vue'
export default {
  name: 'XtxSlider',
  props: {
    // 数据列表
    sliders: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 是否自动轮播
    autoPlay: {
      type: Boolean,
      default: true
    },
    leftShow: {
      type: Boolean,
      default: false
    }
  },
  setup (props) {
    const curIndex = ref(0)
    // 声明一个存放定时器的数据
    const timer = ref(null)
    function clearTimer () {
      clearInterval(timer.value)
    }
    onUnmounted(() => {
      clearInterval(timer.value)
    })
    function startTimer () {
      // 开启定时器  每隔2s中修改一下curIndex的值
      initLoop()
    }
    function add () {
      // 最大能到多大
      // 2  1 -> 2 从2设置为0
      curIndex.value++
      if (curIndex.value === props.sliders.length) {
        curIndex.value = 0
      }
    }
    function initLoop () {
      if (!props.autoPlay) {
        return false
      }
      // 开启定时器  每隔2s中修改一下curIndex的值
      timer.value = window.setInterval(() => {
        add()
      }, 2000)
    }
    function onLeftFn () {
      if (curIndex.value === 0) {
        curIndex.value = props.sliders.length
      }
      curIndex.value--
    }
    function onRightFn () {
      add()
    }
    onMounted(() => {
      initLoop()
    })

    return {
      curIndex,
      clearTimer,
      startTimer,
      onLeftFn,
      onRightFn
    }
  }
}
</script>

<style scoped lang='less'>
.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .slider {
    &-left {
      width: 44px;
      height: 44px;
      background-color: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      opacity: 0;
      transition: all .5s;
      position: absolute;
      left: 270px;
      top: 50%;
      z-index: 2;
      cursor: pointer;
      &::after{
        content:'';
        display: inline-block;
        width: 15px;
        height: 15px;
        border-bottom: 1px solid #fff;
        border-left: 1px solid #fff;
        transform: translate(-50%,-50%) rotate(45deg);
        position: absolute;
        left: 55%;
        top: 50%;
      }
    }
    &-left1{
      left: 20px;
    }
    &-reght {
      width: 44px;
      height: 44px;
      background-color: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      opacity: 0;
      transition: all .5s;
      position: absolute;
      right: 20px;
      top: 50%;
      z-index: 2;
      cursor: pointer;
      &::after{
        content:'';
        display: inline-block;
        width: 15px;
        height: 15px;
        border-bottom: 1px solid #fff;
        border-right: 1px solid #fff;
        transform: translate(-50%,-50%) rotate(-45deg);
        position: absolute;
        right:21% ;
        top: 50%;
      }
    }
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: all 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .slider-btn {
      opacity: 1;
    }
  }
  &:hover {
    .slider-left,
    .slider-reght {
      opacity: 1;
      &:hover{
        background-color: rgba(0, 0, 0, 0.5);
      }
    }
  }
}
</style>
